﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Data.OleDb;
using Aandelendelen.Db;

namespace Aandelendelen.Daily
{
	public class NotActivatedMembers : DailyJob
	{
		public NotActivatedMembers(XmlNode jobNode)
			: base(jobNode, "Deleting not activated members...")
		{ }

		public override void Execute()
		{
			this.OnStart();

			try
			{
				this.DeleteNotActivated();
			}
			catch (Exception e)
			{
				this.OnError(e);
			}

			this.OnEnd();
		}

		private void DeleteNotActivated()
		{
			//verwijder iedereen die zich niet binnen 31 dagen heeft geactiveerd.

			DateTime date = DateTime.Today.AddDays(-31);
			Query query = new Query("s_notactivated", "datum", date, OleDbType.Date);
			ResultSet resultSet = (ResultSet)query.Execute();

			if (resultSet.Count == 0)
				return;

			for (int i = 0; i < resultSet.Count; i++)
			{
				string userName = (string)(resultSet[i, "Gebruikersnaam"]);

				Query query1 = new Query("sp_deleteuserfromactivate", "gebruikersnaam", userName, OleDbType.Char);
				this.queries.Add(query1);

				Query query2 = new Query("sp_deleteuserbyusername", "gebruikersnaam", userName, OleDbType.Char);
				this.queries.Add(query2);
			}
		}
	}
}